package com.android.launcher3.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.telephony.SubscriptionInfo;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.launcher3.R;
import com.x.component.connectivity.ConnectivityComponent;
import com.x.component.connectivity.ConnectivityTool;
import com.x.component.connectivity.IconState;
import com.x.component.connectivity.MobileDataIndicators;
import com.x.component.connectivity.MobileState;
import com.x.component.connectivity.WifiIndicators;
import com.x.component.connectivity.WifiState;

import java.util.List;

public class WifiSettingItemWidget extends SettingItemWidget {

    private String TAG = "WifiSettingItemWidget";

    public WifiSettingItemWidget(@NonNull Context context) {
        super(context);
    }

    public WifiSettingItemWidget(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public WifiSettingItemWidget(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    void setUpView() {
        super.setUpView();
        ConnectivityComponent INSTANCE = ConnectivityComponent.getInstance();
        WifiState initWifiState = INSTANCE.getWifiState();
        if(initWifiState != null){
            if (!initWifiState.enabled) {
                //wifi关闭
                int level = 0;
                icon.setImageLevel(level);

                String close = mContext.getResources().getString(R.string.wifi_close);
                mTvSecName.setText(close);
            } else {
                //wifi 打开
                int level = initWifiState.level;
                icon.setImageLevel(level);
                String ssid = initWifiState.ssid;
                if (TextUtils.isEmpty(ssid)) {
                    ssid = "Unknown";
                }
                mTvSecName.setText(ssid);
            }
        }
        INSTANCE.setConnectivityTool(new ConnectivityTool() {
            @Override
            public void setWifiIndicators(WifiIndicators wifiIndicators) {
                Log.d(TAG, "setCallIndicator");
                WifiState wifiState = wifiIndicators.wifiState;
                if (wifiState != null) {
                    Log.d(TAG, "setWifiIndicators wifiState YYYY" + wifiState);


                    if (!wifiState.enabled) {
                        //wifi关闭
                        int level = 0;
                        icon.setImageLevel(level);

                        String close = mContext.getResources().getString(R.string.wifi_close);
                        mTvSecName.setText(close);
                    } else {
                        //wifi 打开
                        int level = wifiState.level;
                        icon.setImageLevel(level);
                        String ssid = wifiState.ssid;
                        if (TextUtils.isEmpty(ssid)) {
                            ssid = "Unknown";
                        }
                        mTvSecName.setText(ssid);
                    }
                }
            }

            @Override
            public void setMobileDataIndicators(MobileDataIndicators mobileDataIndicators) {
                Log.d(TAG, "setMobileDataIndicators");
                MobileState mobileState = mobileDataIndicators.mobileState;
                if (mobileState != null) {
                    Log.d(TAG, "setMobileDataIndicators mobileState " + mobileState);
                }
            }

            @Override
            public void setSubs(List<SubscriptionInfo> list) {

            }

            @Override
            public void setNoSims(boolean show, boolean simDetected) {

            }

            @Override
            public void setEthernetIndicators(IconState iconState) {

            }

            @Override
            public void setIsAirplaneMode(IconState iconState) {

            }

            @Override
            public void setMobileDataEnabled(boolean enabled) {

            }

            @Override
            public void setConnectivityStatus(boolean noDefaultNetwork, boolean noValidatedNetwork,
                                              boolean noNetworksAvailable) {

            }

            @Override
            public void setCallIndicator(IconState iconState, int subId) {
                ///直接findviewById后更新view呀
                ///使用sdk的图标就这样。
//                icon.setBackground(getResources().getDrawable(iconState.icon));
//
//                ///用自己图标就这样
//
//                if("no_sim_card".equals(iconState.contentDescription)){
//                    icon.setBackgroundResource(R.drawable.ic_no_sim_card);
//                }
////                else if(){
//
////                }


            }
        });
    }
}